/**
 * Copyright 2010 Marcus Calverley <marcus@calverley.dk>
 *
 * This file is part of I'm Busy.
 *
 * I'm Busy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * I'm Busy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with I'm Busy.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.lmz.android.busy;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * Front-end of the application. Allows user to change settings and
 * activate/deactivate calendar monitoring.
 *
 * @author Marcus Calverley <marcus@calverley.dk>
 */
public class SettingsActivity extends PreferenceActivity
{
	/**
	 * Initialise the screen and start the service as appropriate.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// Clear any notifications
		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancelAll();

		// Initialise screen
		addPreferencesFromResource(R.xml.preferences);

		CheckBoxPreference active = (CheckBoxPreference) findPreference("active");
		if (active.isChecked())
		{
			// Make sure service is running when starting program.
			// This is mainly done to set everything up after the application has been updated.
			startService(new Intent(this, BusyService.class));
		}
		active.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
		{
			/**
			 * Starts the service when the Active preference is enabled.
			 */
			public boolean onPreferenceChange(Preference preference, Object newValue)
			{
				if ((Boolean) newValue)
				{
					startService(new Intent(getApplicationContext(), BusyService.class));
				}
				return true;
			}
		});

		// exclude setting needs to change the text on calendars preference
		CheckBoxPreference exclude = (CheckBoxPreference) findPreference("excludeSelectedCalendars");
		if (exclude.isChecked())
		{
			Preference calendars = findPreference("selectedCalendars");
			calendars.setSummary(R.string.calendars_inverse_description);
		}
		exclude.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
		{
			/**
			 * Sets the correct description text on selectedCalendars preference
			 */
			public boolean onPreferenceChange(Preference preference, Object newValue)
			{
				Preference calendars = findPreference("selectedCalendars");

				if ((Boolean) newValue)
				{
					calendars.setSummary(R.string.calendars_inverse_description);
				}
				else
				{
					calendars.setSummary(R.string.calendars_description);
				}
				return true;
			}
		});
	}

	/**
	 * Populate the menu with the options defined for this activity.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.preferences, menu);
		return true;
	}

	/**
	 * Handle user activating menu items.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.about:
				// Show about dialog
				AlertDialog.Builder ab = new AlertDialog.Builder(this);
				ab.setTitle(R.string.app_name);
				ab.setMessage(R.string.about_information);
				ab.setPositiveButton(R.string.ok, new OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
						// Do nothing when OK button is clicked, just close dialog
					}
				});
				ab.show();
				break;
			case R.id.exit:
				// Exit the application
				finish();
				break;
		}
		return true;
	}
}
